<!DOCTYPE html>
<html lang="en">
    <head>
        
        <script src="jquery-1.11.0.js"></script>
        <script src="gen.js"></script>
        <link rel="stylesheet" href="style.css">
        <link href='http://fonts.googleapis.com/css?family=Yanone+Kaffeesatz:700' rel='stylesheet' type='text/css'>
        <title>Health Promotion</title>
        <script>
    //window.location.reload();
    function syncAjax(u){
				var obj=$.ajax(
					{url:u,
					 async:false
					 }
				);
				return $.parseJSON(obj.responseText);
				
			}     
                        
//                        function here(id) {
//                            alert("hjh");
//                          
//                            window.location.reload();
//                        }
//                            
//                            alert("here")
//                            $.ajax({
//                                 type: "POST",
//                                 url: 'health.php',
//                                 success:function (confirmFunction(id)){
//                                 alert(html);
//           }
//
//      });
 
    function add() 
    {
                
                var y = event.clientY;
                var x = event.clientX / 2;
                //use x and y to set the location of the form
                $("#divAdd").css("top", y);
                $("#divAdd").css("left", x);
                //display the form
                $("#divAdd").fadeIn(250);
                //cancel();
            }
            
            var delid;
            function check(id)
            {
                delid = id;
                var y = event.clientY;
                var x = event.clientX / 2;
                $("#confirm").css("top", y);
                $("#confirm").css("left", x);
                $("#confirm").fadeIn(250);
            }
            
            function del()
            { 
              var u="vaccine_action.php?cmd=4&id="+delid; 
              $.getJSON(u,saveDone);
              alert("successfully deleted");
              window.location.reload(); 
            }
            
            function cancel()
            {       
                            $("#divAdd").fadeOut(500);
                            $("#divEdit").fadeOut(500);
                            $("#confirm").fadeOut(500);
				//fade out the form in half a second
				
	    }
                        var id2;
                        function edit($obj,$id)
                         {
                             //alert("edit");
                             //document.write("hello");
                             //alert("thi is id". $id);
                             id2 = $id;
                             
                        var r=getHealth($id);
				if(r.result==0){
                                  document.write("hellw")
					//show error message
					return;
				}
                              
				$("#edittopic").prop("value", r.health.topic);
				$("#editdistrict").prop("value", r.health.subdistrict);
                                $("#editaudience").prop("value", r.health.targetAudience);
                                $("#editvenue").prop("value", r.health.venue);
                                
                                $("#editlongitude").prop("value", r.health.longitude);
                                $("#editlatitude").prop("value", r.health.latitude);
                                $("#editmethod").prop("value", r.health.method);
                                $("#editnumAppearance").prop("value", r.health.numAudience);
                                $("#editremarks").prop("value", r.health.remarks);
                                $("#editfile").prop("value", r.health.file);
                                $("#editdate").prop("value", r.health.date);
                                $("#editmonth").prop("value", r.health.month);
				//show the form
				//find where the user clicked and store it in x and y
				var y=event.clientY;
				var x=event.clientX/2;
				//use x and y to set the location of the form
				$("#divEdit").css("top",y);
				$("#divEdit").css("left",x);
				//display the form
				$("#divEdit").fadeIn(1000);
			}
			//returns a result object for one vaccine 
			function getHealth(id){
				var u="vaccine_action.php?cmd=1&id="+id;
				return syncAjax(u);
			}
                        
                        function refresh()
                        {
                          window.location.reload();
                        }
                        
                        function update()
                        {
                           
                            var topic = document.getElementById("edittopic").value;
                            var district = document.getElementById("editdistrict").value; 
                           
                            var audience = document.getElementById("editaudience").value;
                            
                            var venue = document.getElementById("editvenue").value; 
                           
                            var longitude = document.getElementById("editlongitude").value;  
                            var latitude = document.getElementById("editlatitude").value;
                            var method = document.getElementById("editmethod").value;
                            var numApp = document.getElementById("editnumAppearance").value;
                            var remarks = document.getElementById("editremarks").value;
                            var file = document.getElementById("editfile").value;
                            var date = document.getElementById("editdate").value;
                           
                            var month = document.getElementById("editmonth").value;
                            //document.write(id2);
                          
				//complete the url
				var u="vaccine_action.php?cmd=2&id="+id2+"&topic="+topic+"&district="+district+"&audience="+audience+
                                        "&venue="+venue+"&long="+longitude+"&lat="+latitude+"&method ="+method+"&numApp ="+numApp+
                                        "&remarks="+remarks+"&file="+file+"&date="+date+"&month="+month;
                                
				$.getJSON(u,saveDone);
                                
                                cancel();
                        }
                        
                        function saveDone(data){
				
				alert(data);
			}
                        
    


            
        </script>
    </head>

    <body id = "background">
        <?php
        include_once 'healthclass.php';
        include_once 'subdistrict.php';
        include_once 'choclass.php';
        ?>
        
        <div id = "homepage">
            <table style ="float: left; width: 150px">

                <tr>
                    <td>
                        <div id = "navigation">
                        </div>  
                    </td>
                </tr>

            </table>
            <table>
                <tr>

                    <td>
                        <div id = "content">
<!--                            <div id="pageheader" style="background-color: #558C89">

                            </div>-->
 <div id="pageheader" style="padding-top: 20px; border">
                                <img src ="Images/healthrealPAINT.jpg">
                            </div>
                            <br>
                            <br>
                            <br>
                            <br>
                            <br>

                            <div id = "searchBar" >
                                
                                <div style="float:left">
                                   <input  type="button" onclick="add()" id = "button" value = "Add new promotion" name = "add">
                                   </div>
                                <div style="float:left">
                                   <form id ="searchAdd">
                                       
                            <!--<input class="form-control" name = "searchText" id = "searchText">-->
                             <!--<input type="submit" id = "button" value = "Search" name = "search">-->
                        
                                    <input type="text" name ="searchText" id = "searchText">
                                    <input type="submit" id = "button" value = "Search" name = "search">
                                   </form> 
                                </div>
                                   
                            </div>
                            <div style="float: left; padding-left: 20px ">
                                <a class="mainMenu" href="index_1.php">Dashboard</a>
                                <a class="mainMenu" href="health.php">Health Promotions</a>
                                <a class="mainMenu" href="charts.php">Questions And Answers</a>

                            </div>
                            
                            <br>
                            <div style ="float: left; padding-left: 30px">
                                <table class="reportTable">
                                    <thead>
                                        <tr class="row2">
                                            <th></th>
                                            <th>CHO <i class="fa fa-sort"></i></th>
                                            <th>Topic <i class="fa fa-sort"></i></th>
                                            <th>Subdistrict <i class="fa fa-sort"></i></th>
                                            <th>Date <i class="fa fa-sort"></i></th>
                                            <th> <i class="fa fa-sort"></i></th>
                                            <th> <i class="fa fa-sort"></i></th>
                                        </tr>
                                    </thead>
                                    <tbody>

                                        <?php
                                        $health = new healthclass();
                                        $cho = new choclass();
                                        $subd = new subdistrict();
                                        $chorow = "";
                                        $subdrow = "";
                                        $row_counter = 1;

                                        if (isset($_REQUEST['searchText'])) {
                                            $searchrow = $health->search_health($_REQUEST['searchText']);
                                            $searchrow = $health->fetch();

                                            while ($searchrow) {
                                                if ($row_counter % 2 == 0) {
                                                    $style = " class='row1' ";
                                                } else {
                                                    $style = " class='row2'  ";
                                                }
                                                echo"<tr id ='row' $style>";
                                                $cho->get_cho_by_id($searchrow['idcho']);
                                                $chorow = $cho->fetch();
                                                echo"<td>$row_counter</td>";
                                                echo"<td id='id'>$chorow[cho_name]</td>";
                                                echo"<td id='id'>$searchrow[topic]</td>";
                                                $subd->get_subdistrict_by_id($searchrow['subdistrict_id']);
                                                $subdrow = $subd->fetch();
                                                echo"<td id='id'>$subdrow[subdistrict_name]</td>";
                                                echo"<td id='id'>$searchrow[date]</td>";
                                                $hid = $searchrow['idhealth_promotion']; 
                                                echo"<td><span class='hotspot' onclick ='edit(this,$hid)'>Edit<span></td>";
                                                echo"<td><span class='hotspot' onclick ='check($hid)'>Del<span></td>";
                                                //echo"<td id='id'><a href ='' onclick = 'add()'>View details</a></td>";
                                                echo"</tr>";
                                                $row_counter++;
                                                $searchrow = $health->fetch();
                                            }
                                        }
                                       
                                        else {
                                            $row = $health->get_all_health();
                                            $row = $health->fetch();
                                            while ($row) {
                                                if ($row_counter % 2 == 0) {
                                                    $style = " class='row1' ";
                                                } else {
                                                    $style = " class='row2'  ";
                                                }
                                                echo"<tr $style>";
                                                $cho->get_cho_by_id($row['idcho']);
                                                $chorow = $cho->fetch();
                                                echo"<td>$row_counter</td>";
                                                echo"<td id='id'>$chorow[cho_name]</td>";
                                                echo"<td id='id'>$row[topic]</td>";
                                            
                                                $subd->get_subdistrict_by_id($row['subdistrict_id']);

                                                $subdrow = $subd->fetch();
                                                echo"<td id='id'>$subdrow[subdistrict_name]</td>";
                                                echo"<td id='id'>$row[date]</td>";
                                                $hid = $row['idhealth_promotion']; 
                                                echo"<td><span class='hotspot' onclick ='edit(this,$hid)'>Edit<span></td>";
                                                echo"<td><span class='hotspot' onclick ='check($hid)'>Del<span></td>";
                                                //echo"<td id='id'><a href ='' onclick = 'add()'>View details</a></td>";
                                                echo"</tr>";
                                                $row_counter++;
                                                $row = $health->fetch();
                                            }
                                        }
                                        ?>
                                </table>

                            </div>
                        </div>
                    </td>


                </tr>
            </table>
            <?php ?>

            <div id = "divAdd" class="popupForm">
                <form role="form">
                    <div class="col-lg-6">
                        <div style="float: left; padding-left: 15px; padding-top: 15px">
                        <div class="form-group">
                            <label class="label">Topic</label>
                            <input class="form-control" name = "topic">
                        </div>

                        <div class="form-group">
                            <label class="label">Subdistrict</label>
                            <?php
                            $district = new subdistrict();
                            $district->get_all_subdistricts();
                            $row = $district->fetch();
                            echo"<select class='form-control' name = 'district'>";
                            while ($row) {

                                echo"<option value =$row[subdistrict_id]>$row[subdistrict_name]</option>";
                                $row = $district->fetch();
                            }

                            echo"</select>";
                            ?>

                        </div>

                        <div class="form-group">
                            <label class="label">Target Audience</label>
                            <input class="form-control" id="audience" name = "audience">
                        </div>

                        <div class="form-group">
                            <label class="label">Venue</label>
                            <input class="form-control"  name ="venue">
                        </div>

                        <div class="form-group">
                            <label class="label">Longitude
                            </label><br>
                            <input class="form-control" name = "longitude">
                        </div>

                        <div class="form-group">
                            <label class="label">Latitude
                            </label><br>
                            <input class="form-control" name = "latitude">
                        </div>
                        </div>
                        
                        <div style = "float: left; padding-left: 100px; padding-top: 15px"> 
                        <div class="form-group">
                                <label class="label">Method</label>
                                <textarea class="form-control" name = "method" rows ="4" cols = "40"></textarea>
                            </div>

                            <div class="form-group">
                                <label class="label">Number of Appearances
                                </label><br>
                                <input class="form-control" name = "numappearances">
                            </div>

                            <div class="form-group">
                                <label class="label">Remarks
                                </label><br>
                                <textarea class="form-control" name ="remarks" rows = 4 cols="40"></textarea>
                            </div>

                            <!--              <div class="form-group">
                                            <label>Static Control</label>
                                            <p class="form-control-static">email@example.com</p>
                                          </div>-->

                            <div class="form-group">
                                <label class="label">Choose an image</label>
                                <input type="file" name = "file">
                            </div>

                            <div class="form-group">
                                <label class="label">Date</label>
                                <input class="form-control" type="date" name = "date">
                            </div>

                            <div class="form-group">
                                <label class="label">Month</label>
                                <select class='form-control' name = 'month'>

                                    <option value="January">Jan</option>
                                    <option value="Febuary">Feb</option>
                                    <option value="March">Mar</option>
                                    <option value="April">Apr</option>
                                    <option value="May">May</option>
                                    <option value="June">Jun</option>
                                    <option value="July">Jul</option>
                                    <option value="August">Aug</option>
                                    <option value="September">Sep</option>
                                    <option value="October">Oct</option>
                                    <option value="November">Nov</option>
                                    <option value="December">Dec</option>
                                </select>

                            </div>
                            
                            <div id ="subCancel">
                                <input type="submit" value = "Submit" onclick="refresh()">
                            <input type="button" value="Cancel" onclick="cancel()" >
                            </div>
                         
                        
                    </div>
                    </div>
                </form>
            </div>
            
            <div id = "divEdit" class="popupForm" >
                <form role="form">
                    <div class="col-lg-6">
                        <div style="float: left; padding-left: 15px; padding-top: 15px">
                        <div class="form-group">
                            <label class="label">Topic</label>
                            <input class="form-control" name = "edittopic" id ="edittopic">
                        </div>

                            <div class="form-group">
                            <label class="label">Subdistrict</label>
                            <?php
                            $district = new subdistrict();
                            $district->get_all_subdistricts();
                            $row = $district->fetch();
                            echo"<select class='form-control' name = 'editdistrict' id = 'editdistrict'>";
                            while ($row) {

                                echo"<option value =$row[subdistrict_id]>$row[subdistrict_name]</option>";
                                $row = $district->fetch();
                            }

                            echo"</select>";
                            ?>

                        </div>
<!--                        

-->                        <div class="form-group">
                            <label class="label">Target Audience</label>
                            <input class="form-control" id="editaudience" name = "editaudience">
                        </div><!--

                            //<?php
                            echo"</select>";
                            ?>

                        </div>-->
                        <div class="form-group">
                            <label class="label">Venue</label>
                            <input class="form-control" id ="editvenue" name ="editvenue">
                        </div>

                        <div class="form-group">
                            <label class="label">Longitude
                            </label><br>
                            <input class="form-control" name = "editlongitude" id ="editlongitude">
                        </div>

                        <div class="form-group">
                            <label class="label">Latitude
                            </label><br>
                            <input class="form-control" name = "editlatitude" id ="editlatitude">
                        </div>
                        </div>
                        
                        <div style = "float: left; padding-left: 100px; padding-top: 15px"> 
                        <div class="form-group">
                                <label class="label">Method</label>
                                <textarea class="form-control" id ="editmethod" name = "editmethod" rows ="4" cols = "40"></textarea>
                            </div>

                            <div class="form-group">
                                <label class="label">Number of Appearances
                                </label><br>
                                <input class="form-control" name = "editnumappearances" id ="editnumAppearance">
                            </div>

                            <div class="form-group">
                                <label class="label">Remarks
                                </label><br>
                                <textarea class="form-control" id ="editremarks" name ="editremarks" rows = 4 cols="40"></textarea>
                            </div>

                            <!--              <div class="form-group">
                                            <label>Static Control</label>
                                            <p class="form-control-static">email@example.com</p>
                                          </div>-->

                            <div class="form-group">
                                <label class="label">Choose an image</label>
                                <input type="file" name = "editfile" id ="editfile">
                            </div>

                            <div class="form-group">
                                <label class="label">Date</label>
                                <input class="form-control" type="date" name = "editdate" id ="editdate">
                            </div>

                            <div class="form-group">
                                <label class="label">Month</label>
                                <select class="form-control" name = "editmonth" id ="editmonth">

                                    <option value="January">Jan</option>
                                    <option value="Febuary">Feb</option>
                                    <option value="March">Mar</option>
                                    <option value="April">Apr</option>
                                    <option value="May">May</option>
                                    <option value="June">Jun</option>
                                    <option value="July">Jul</option>
                                    <option value="August">Aug</option>
                                    <option value="September">Sep</option>
                                    <option value="October">Oct</option>
                                    <option value="November">Nov</option>
                                    <option value="December">Dec</option>
                                </select>

                            </div>
                            
                            <div id ="subCancel">
                            <input type="submit" value = "Update" onclick="update()">
                            <input type="button" value="Cancel" onclick="cancel()" >
                            </div>
                            <!--<button type="reset" class="btn btn-default">Reset</button>--> 
                        
                        
                    </div>
                    </div>
                </form>
            </div>
        </div>
         <?php
        if (isset($_POST)) {
            $topic = "";
            if (isset($_REQUEST['topic'])) {
                $topic = ($_REQUEST['topic']);
            }

            $districtid = "";
            if (isset($_REQUEST['district'])) {
                $districtid = ($_REQUEST['district']);
            }

            $audience = "";
            if (isset($_REQUEST['audience'])) {
                $audience = ($_REQUEST['audience']);
            }

            $venue = "";
            if (isset($_REQUEST['venue'])) {
                $venue = ($_REQUEST['venue']);
            }

            $longitude = "";
            if (isset($_REQUEST['longitude'])) {
                $longitude = ($_REQUEST['longitude']);
            }

            $latitude = "";
            if (isset($_REQUEST['latitude'])) {
                $latitude = ($_REQUEST['latitude']);
            }

            $method = "";
            if (isset($_REQUEST['method'])) {
                $method = ($_REQUEST['method']);
            }

            $numapp = "";
            if (isset($_REQUEST['numappearances'])) {
                $numapp = ($_REQUEST['numappearances']);
            }

            $remarks = "";
            if (isset($_REQUEST['remarks'])) {
                $remarks = ($_REQUEST['remarks']);
            }

            $date = "";
            if (isset($_REQUEST['date'])) {
                $date = ($_REQUEST['date']);
            }

            $month = "";
            if (isset($_REQUEST['month'])) {
                $month = ($_REQUEST['month']);
            }

            $image = "";
            if (isset($_REQUEST['file'])) {
                $image = $_REQUEST['file'];
            }



            $promotion = new healthclass();
            $promotion->add_promotion($date, $venue, $topic, $method, $audience, $numapp, $remarks, $month, $latitude, $longitude, $image, 1, $districtid);
           
        }
        ?>
<!--         <script>
             window.location.reload();
            </script>-->
        <div id = "confirm" class="popupForm">
            <div id = "questionConfirm">
                 Are you sure you want to delete?
            </div>
           
            <form id = "confirmForm">
                <input type ="submit" onclick="del()" value="YES">
                <input type="submit" onclick="cancel()" value="NO">
            </form>
                </div>
        <?php
        
        
        
//        function confirmFunction($key)
//        {
//            echo "i am here";
//            echo "<div id = 'confirm' style = 'background-color:red' class='popupForm'>";
//            echo "Are you sure you want to delete?";
//               echo"<div style ='float: left'>";
//                   echo"<button onclick ='del($key)'> Yes <button>";   
//                   echo "</div>";
//                   
//                   echo"<div style ='float: left'>";
//                   echo"<button onclick ='cancel()'> No <button>";   
//                   echo "</div>";
//                   
//               echo"</div>";
//        }
        ?>
    </body>
</html>


  